
$(function() {
	//$.NotificationApp.send("提示","ook","top-center","rgba(0,0,0,0.2)","success");  //error success warning info
	
	$(".bind-sncode-success-close").click(function(){
		$(this).parents(".modal").removeClass("show").css("display", "none");
		window.location.reload();
	});
	
	$(".close,.closeModal").click(function(){
		$(this).parents(".modal").removeClass("show").css("display", "none");
	});
    
	//绑定设备
	$(".bind-snCode").click(function(){
		var errP = $("#bsErrMsg");
		
		var snName = $.trim($("#snName").val());
		if(snName == ""){
			errP.html("请输入矿场名称");
			return;
		}
		
		var sn = "";
		var snCode = $(".snCode");
		for(var i = 0 ; i < snCode.length ; i ++){
			var code = $.trim(snCode.eq(i).val());
			if(code == "" || code.length != 4){
				errP.html("第" + (i + 1) + "个SN码输入有误");
				return;
			}
			sn = sn + code;
		}
		errP.html("&nbsp;");
		$.ajax({
			type : "post",
			url : basePath + "/equipment/bindEquipment",
			data : {"snName":snName,"sn":sn},
			dataType: "json",
			success : function(res){
				if(res.bizSuccess){
					$("#sn-bind").removeClass("show").css("display", "none");
					$("#bind-sncode-success").addClass("show").css("display", "block");
					setTimeout(function(){
						$(this).removeClass("show").css("display", "none");
						window.location.reload();
					}, 3000);
				}
				else{
					errP.html(res.errorMsg);
				}
			}
		});
	});
	
	//解绑设备  提示
	$(".unbindEquip").click(function(){
		//if(!checkSN($(this))) return;
		var eID = $(this).attr("equipID");
		$("#confirmUnBind").attr("equipID", eID);
		$("#unbind" + eID).trigger("click");
		$("#unbind" + eID).click();
	});
	
	//确认解绑 confirmUnBind
	$("#confirmUnBind").click(function(){
		var equipID = $(this).attr("equipID");
		if(typeof(equipID) == "undefined" || equipID == 0){
			return ;
		}
		$.ajax({
			type : "post",
			url : basePath + "/equipment/unbindEquipment",
			data : {"equipID":equipID },
			dataType: "json",
			success : function(res){
				if(res.bizSuccess){
					$("#warning-header-modal").removeClass("show").css("display", "none");
					window.location.reload();
				}
				else{
					errMsg(res.errorMsg);
				}
			}
		});
		
	});
	
	//正常 bg-success  关闭 bg-secondary  异常 bg-danger
	//改变或打开设备
	$(".closeEquip").click(function(){
		var $this = $(this);
		
		if(!checkSN($this)) return;
		var card = $this.parents(".card");
		var equipID = $this.attr("equipID");
		var isClose = $this.attr("isClose"); 
		if(equipID == "" || isClose == ""){
			return ;
		}
		
		var msg = "";
		if(isClose == 1){
			isClose = 0;
			msg = "设备打开成功";
		}
		else{
			isClose = 1;
			msg = "设备关闭成功";
		}
		
		$.ajax({
			type : "post",
			url : basePath + "/equipment/changeEquipmentStatus",
			data : {"equipID":equipID, "isClose" : isClose },
			dataType: "json",
			success : function(res){
				if(res.bizSuccess){
					$.NotificationApp.send("提示", msg ,"top-center","rgba(0,0,0,0.2)","success");
					$this.attr("isClose", isClose);
					if(isClose == 1){
						card.removeClass("bg-success").addClass("bg-secondary");
						$this.attr("data-original-title","打开");
					}
					else{
						card.removeClass("bg-secondary").addClass("bg-success");
						$this.attr("data-original-title","关闭");
					}
				}
				else{
					errMsg(res.errorMsg);
				}
				
			}
		});
	});
	
	//添加矿机设置
	$("#add-worker-btn").click(function(){
		var errP = $("#awErrMsg");
		var workerType = $.trim($("#workerTypeAdd").val());
		if(workerType == ""){
			errP.html("请输入型号");
			return;
		}
		var workerName = $.trim($("#workerName").val());
		if(workerName == ""){
			errP.html("请输入用户");
			return;
		}
		var workerPwd = $.trim($("#workerPwd").val());
		if(workerPwd == ""){
			errP.html("请输入密码");
			return;
		}
		
		errP.html("&nbsp;");
		$.ajax({
			type : "post",
			url : basePath + "/workerSet/addWorkerSet",
			data : {"workerType":workerType,"workerName":workerName,"workerPwd":workerPwd},
			dataType: "json",
			success : function(res){
				if(res.bizSuccess){
					window.location.reload();
				}
				else{
					errP.html(res.errorMsg);
				}
			}
		});
	});
	
	$(".allCheck").click(function(){
		var check = $(this).prop("checked");
		$(this).parents(".table").find("input").prop("checked", check);
	});
	
	//删除矿机设置
	$(".delWorkerSet").click(function(){
		var workerSetIDs = "";
		$(".single").each(function(){
			if($(this).prop("checked")){
				workerSetIDs = workerSetIDs + $(this).val() + ",";
			}
		});
		
		if(workerSetIDs == ""){
			errMsg("请选择要删除的矿机设置");
			return;
		}
		
		workerSetIDs = workerSetIDs.substring(0, workerSetIDs.length - 1);
		$("#confirmDelSet").attr("workerSetIDs",workerSetIDs);
	    $("#hiddenSetBtn").trigger("click");
	});
	
	//确认删除
	$("#confirmDelSet").click(function(){
		var workerSetIDs = $(this).attr("workerSetIDs");
		$.ajax({
			type : "post",
			url : basePath + "/workerSet/delWorkerSet",
			data : {"workerSetIDs":workerSetIDs},
			dataType: "json",
			success : function(res){
				if(res.bizSuccess){
					window.location.reload();
				}
				else{
					errMsg(res.errorMsg);
				}
			}
		});
	});
	
	//修改矿机设置
	$(".workerSetTable td").not(".notHover").mouseenter(function(){
		setMouseOver($(this));
	});
	
	$(".workerSetTable td").not(".notHover").mouseleave(function(){
		setMouseOut($(this))
	});
	
	$(".workerSetTable td").not(".notHover").click(function(){
		$(this).unbind("mouseleave");
	});
	
	$(".task-list-items").on("blur", ".workerSetTable td  input", function(){
		$(this).bind("mouseleave");
		setMouseOut($(this).parents("td"));
	});
	
	//添加矿机预警
	$("#worker-warn-btn").click(function(){
		var errP = $("#aaErrMsg");
		var workerType = $.trim($("#workerType").val());
		if(workerType == ""){
			errP.html("请输入型号");
			return;
		}
		var warnValue = $.trim($("#warnValue").val());
		if(warnValue == ""){
			errP.html("请输入预警算力值");
			return;
		}
		errP.html("&nbsp;");
		$.ajax({
			type : "post",
			url : basePath + "/workerWarn/addWorkerWarn",
			data : {"workerType":workerType,"warnValue":warnValue},
			dataType: "json",
			success : function(res){
				if(res.bizSuccess){
					window.location.reload();
				}
				else{
					errP.html(res.errorMsg);
				}
			}
		});
	});
		
	
	//修改矿机预警
	$(".workerWarnTable td").not(".notHover").mouseenter(function(){
		warnMouseOver($(this));
	});
	
	$(".workerWarnTable td").not(".notHover").mouseleave(function(){
		warnMouseOut($(this))
	});
	
	$(".workerWarnTable td").not(".notHover").click(function(){
		$(this).unbind("mouseleave");
	});
	
	$(".task-list-items").not(".notHover").on("blur", ".workerWarnTable td", function(){
		$(this).bind("mouseleave");
		warnMouseOut($(this));
	});
	
	
	//删除矿机预警记录
	$(".delWorkerWarn").click(function(){
		var workerWarnIDs = "";
		$(".singleTwo").each(function(){
			if($(this).prop("checked")){
				workerWarnIDs = workerWarnIDs + $(this).val() + ",";
			}
		});
		
		if(workerWarnIDs == ""){
			$.NotificationApp.send("提示","请选择要删除的矿机预警记录","top-center","rgba(0,0,0,0.2)","warning");
			return;
		}
		
		workerWarnIDs = workerWarnIDs.substring(0, workerWarnIDs.length - 1);
		
		$("#confirmDelWarn").attr("workerWarnIDs",workerWarnIDs);
	    $("#hiddenWarnBtn").trigger("click");
		
	});
	
	//删除矿机预警
	$("#confirmDelWarn").click(function(){
		var workerWarnIDs = $(this).attr("workerWarnIDs");
		$.ajax({
			type : "post",
			url : basePath + "/workerWarn/delWorkerWarn",
			data : {"workerWarnIDs":workerWarnIDs},
			dataType: "json",
			success : function(res){
				if(res.bizSuccess){
					window.location.reload();
				}
				else{
					errMsg(res.errorMsg);
				}
			}
		});
	});
});

var fir = "";
function setMouseOver($this){
	fir = $.trim($this.text());
	var pwd = $this.attr("pwd");
	var t = $this.attr("t");// 
	fir = t == "workerPwd" ? pwd : fir;
	var a = '<input type="text" value="'+ fir + '" class="form-control" >';
	var hl = $this.html()
	if(hl.indexOf("form-control") < 0){
		$this.html(a);
	}
}

function setMouseOut($this){
	var clazz = $this.attr("class");
	if(typeof(clazz) != "undefined" && clazz.indexOf("notHover") != -1){
		return;
	}
	var workerSetID = $this.parents("tr").attr("setID");
	var tex = $.trim($this.find("input").val());
	var pwd = $this.attr("pwd");
	var t = $this.attr("t");
	
	isPwd = t == "workerPwd" ;
	if(isPwd){
		$this.attr("pwd", tex);
	}
	$this.html(isPwd ? "******" : tex);
	
	//变量形式参数
	var data = {};
	data[t] = tex;
	data["workerSetID"] = workerSetID;
	
	if(fir != tex){
		$.ajax({
			type : "post",
			url : basePath + "/workerSet/modWorkerSet",
			data : data,
			dataType: "json",
			success : function(res){
				if(res.bizSuccess){
					$this.attr("pwd", tex);
				}
				else{
					$this.attr("pwd", pwd);
					errMsg(res.errorMsg);
				}
			}
		});
	}
}

function warnMouseOut($this){
	var clazz = $this.attr("class");
	if(typeof(clazz) != "undefined" && clazz.indexOf("notHover") != -1){
		return;
	}
	
	var workerWarnID = $this.parents("tr").attr("warnID");
	var tex = $.trim($this.find("input").val());
	var t = $this.attr("t");
	
	$this.html(tex);
	if(sec != tex){
		//变量形式参数
		var data = {};
		data[t] = tex;
		data["workerWarnID"] = workerWarnID;
		
		$.ajax({
			type : "post",
			url : basePath + "/workerWarn/modWorkerWarn",
			data : data,
			dataType: "json",
			success : function(res){
				if(res.bizSuccess){
				}
				else{
					errMsg(res.errorMsg);
				}
			}
		});
	}
}

var sec = "";
function warnMouseOver($this){
	sec = $.trim($this.text());
	var a = '<input type="text" value="'+ sec + '" class="form-control" >';
	var hl = $this.html()
	if(hl.indexOf("form-control") < 0){
		$this.html(a);
	}
}

function checkSN($this){
	var isOff = $this.attr("isOff");
	if(isOff == 1){
		warnMsg("当前设备已离线，请稍后操作");
		return false;
	}
	return true;
}


